from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class GradeDetail(models.Model):
    """成绩明细表 - 对应grade表（课程成绩）"""
    student = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='grades',
        verbose_name='学生',
        db_comment='学生ID'
    )
    course_no = models.IntegerField(null=True, blank=True, verbose_name='课程号', db_comment='课程编号')
    course_name = models.CharField(max_length=255, blank=True, verbose_name='课程名', db_comment='课程名称')
    grade = models.CharField(max_length=20, blank=True, verbose_name='分数', db_comment='课程成绩')
    time = models.IntegerField(null=True, blank=True, verbose_name='学年', db_comment='学年')
    
    created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name='创建时间', db_comment='创建时间')
    updated_at = models.DateTimeField(auto_now=True, null=True, blank=True, verbose_name='更新时间', db_comment='更新时间')
    
    class Meta:
        db_table = 'zongce_grade_detail'
        verbose_name = '成绩明细'
        verbose_name_plural = '成绩明细'
        ordering = ['-time', 'student']
        unique_together = [['student', 'course_no', 'time']]
        db_table_comment = '成绩明细表，存储学生的课程成绩信息'
    
    def __str__(self):
        return f"{self.student.username} - {self.course_name} - {self.grade}"
